<template>
  <div class="gi_table_page">
    <GiTable
      row-key="serialNumber"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1200 }"
      :pagination="false"
      :summary="summaryData"
      @cell-click="cellRow"
      @refresh="search"
    >
      <template #top>
        <GiForm v-model="queryForm" search :columns="queryFormColumns" size="medium" @search="search" @reset="reset"></GiForm>
      </template>
      <template #toolbar-right>
        <a-button @click="onExport">
          <template #icon><icon-download /></template>
          <template #default>导出</template>
        </a-button>
      </template>
    </GiTable>
  </div>
</template>

<script setup lang="ts">
import { Message, type TableInstance } from '@arco-design/web-vue'
import { reactive, ref } from 'vue'
import { exportStoreData, fetchStoreData } from '@/apis/cakerun/storeData'
import { useDownload, useTable } from '@/hooks'
import type { ColumnItem } from '@/components/GiForm'
import { listAllBrand } from '@/apis/cakerun/brandAccount'

const brandInfo = ref<any[]>([])
const now = new Date()
now.setDate(now.getDate() - 1)
const queryForm = reactive({
  accountIds: [] as string[],
  statisticsDate: now.toISOString().slice(0, 10),
})

// 品牌和门店选项加载
const getBrandInfos = async () => {
  const res = await listAllBrand({ enable: true })
  brandInfo.value = res.data.map((item) => ({ label: item.brand, value: item.id }))
}
const queryFormColumns: ColumnItem[] = [
  {
    type: 'select',
    label: '品牌',
    field: 'accountIds',
    props: {
      options: brandInfo,
      placeholder: '请选择品牌',
      multiple: true,
      allowClear: false,
    },
  },
  {
    type: 'date-picker',
    label: '统计日期',
    field: 'statisticsDate',
    props: {
      format: 'YYYY-MM-DD',
      allowClear: false,
    },
  },
]

// 表格字段
const columns: TableInstance['columns'] = [
  { title: '门店城市', dataIndex: 'storeCity', width: 100 },
  {
    title: '名单名称',
    slotName: 'shopName',
    dataIndex: 'shopName',
    width: 150,
  },
  { title: '数量', dataIndex: 'totalCount', slotName: 'totalCount', width: 100 },
  { title: '商家应得', dataIndex: 'merchantIncome', slotName: 'merchantIncome', width: 100 },
  { title: '4英寸蛋糕', dataIndex: 'cake4inchCount', slotName: 'cake4inchCount', width: 100 },
  { title: '收益', dataIndex: 'cake4inchIncome', slotName: 'cake4inchIncome', width: 100 },
  { title: '生日蛋糕', dataIndex: 'birthdayCakeCount', slotName: 'birthdayCakeCount', width: 100 },
  { title: '收益', dataIndex: 'birthdayCakeIncome', slotName: 'birthdayCakeIncome', width: 100 },
]
// 表格数据
const {
  tableData: dataList,
  loading,
  search,
  summary,
} = useTable(async () => {
  if (!queryForm.accountIds || queryForm.accountIds.length === 0) {
    await getBrandInfos()
    queryForm.accountIds = [brandInfo.value[0].value]
  }
  return fetchStoreData(queryForm)
}, { immediate: true })

const reset = () => search()

// eslint-disable-next-line no-empty-pattern
const summaryData = ({}) => {
  return summary.value
}

const cellRow = (record: any, column: any) => {
  const data = record[column.dataIndex]
  if (data) {
    navigator.clipboard.writeText(data.toString()).then(() => {
      Message.success('复制成功')
    })
  }
}

const onExport = () => {
  Message.info('文件正在下载中，请稍后.....')
  useDownload(() => exportStoreData(queryForm))
}
</script>

<style scoped lang="scss">
:deep(.arco-table-td-content){
  cursor: pointer;
}
.gi_table_page {
  padding: 16px;
  background: #fff;
  height: 100%;
  box-sizing: border-box;
}

// 嵌套表格样式优化
:deep(.nested-table) {
  width: 100% !important;
  margin: 0;

  .arco-table-td {
    padding: 4px 8px !important;  // 减少内边距，更紧凑
    border-color: #f0f0f0 !important;
  }

  .arco-table-row:hover .arco-table-td {
    background-color: #fafafa !important;  // 子表格行hover效果
  }
}

// 空状态样式
.empty-cake-sales {
  text-align: center;
  padding: 8px 0;
  color: #86909c;
  font-size: 12px;
}

// 合计行样式
:deep(.total-row) {
  font-weight: bold !important;
  color: #165dff !important;
  background-color: #f0f7ff !important;
}

// 优化单元格点击反馈
:deep(.arco-table-td-content) {
  cursor: pointer;
  &:hover {
    background-color: #f5f7fa;
  }
}

// 调整滚动区域样式
:deep(.arco-table-body-wrapper) {
  &::-webkit-scrollbar {
    width: 6px;
    height: 6px;
  }
  &::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 3px;
  }
}
</style>
